home *** CD-ROM | disk | FTP | other *** search
/ Ian & Stuart's Australian Mac 1993 September / September 93.iso / Archives / Games / Strategy / Puzzle / GameMaster / GM Dev Kit / Rulebook Sources / RuleBook ƒ / RuleBook.p < prev    next >
Encoding:
Text File  |  1991-11-10  |  8.3 KB  |  390 lines  |  [TEXT/PJMM]

  1. unit RuleBook;
  2.  
  3. { Generic Rule Book © Peter Lewis, Oct 1991 }
  4. { This program and its source are (is? who knows) Povertyware }
  5.  
  6. interface
  7.  
  8.     uses
  9.         QuickDrawRules, GameTypes;
  10.  
  11.     procedure Main (var ger: gameEventRecord);
  12.  
  13. implementation
  14.  
  15.     const
  16.         game_dlog = 1000;            { The DLOG resource specifying the window size }
  17.         game_dialog_item = 1;        { The base Dialog Item used to do drawing }
  18.  
  19. { Game Specific constants and types: }
  20.     const
  21.         rect_max = 4;                    { Number of rectangles to click in }
  22.         game_status_item = 2;        { The status item }
  23.         status_click = 1;
  24.         status_you = 2;
  25.         status_waiting = 3;
  26.         game_strh = 1000;
  27.  
  28.     type
  29.         rects = 1..rect_max;
  30.  
  31.     type
  32.         globalsRecord = record
  33.                 item_rect: rect;
  34. { Game Specific global variables: }
  35.                 therects: array[rects] of rect;
  36.             end;
  37.         globalsPeek = ^globalsRecord;
  38.         gameRecord = record
  39.                 globals: globalsPeek;
  40.                 playing: boolean;
  41.                 connected: boolean;
  42. { Game Specific game variables: }
  43.                 state: rects;
  44.                 myturn: boolean;
  45.             end;
  46.         gamePeek = ^gameRecord;
  47.  
  48. {Game Specific drawing routines:}
  49.  
  50.     procedure DrawTheRect (ggame: gamePeek; i: rects);
  51.         var
  52.             r: rect;
  53.     begin
  54.         r := ggame^.globals^.therects[i];
  55.         FrameRect(r);
  56.         InsetRect(r, 1, 1);
  57.         if i = ggame^.state then
  58.             FillRect(r, QDGlobals^.grey)
  59.         else
  60.             EraseRect(r);
  61.     end;
  62.  
  63.     procedure DrawGame (gwindow: windowPtr; ggame: gamePeek; gglobals: globalsPeek);
  64. { Draw the game inside item_rect }
  65.         var
  66.             i: rects;
  67.     begin
  68.         for i := 1 to 4 do
  69.             DrawTheRect(ggame, i);
  70.     end;
  71.  
  72.     procedure DrawGameProc (wp: windowPtr; item: integer);
  73. { Touch Not: This is the update proc for game_dialog_item }
  74.         var
  75.             h: handle;
  76.     begin
  77.         h := handle(GetWRefCon(wp));
  78.         HLock(h);
  79.         DrawGame(wp, gamePeek(h^), gamePeek(h^)^.globals);
  80.         HUnlock(h);
  81.     end;
  82.  
  83.     procedure Main (var ger: gameEventRecord);
  84.         var
  85.             gwindow: windowPtr;
  86.             ghandle: handle;
  87.             ggame: gamePeek;
  88.             gglobals: globalsPeek;
  89.  
  90.         procedure RedrawGame;
  91.         begin
  92.             DrawGame(gwindow, ggame, gglobals);
  93.         end;
  94.  
  95. {Game Specific routines:}
  96.  
  97.         procedure SetItemText (dlg: dialogPtr; item: integer; text: str255);
  98.             var
  99.                 it: integer;
  100.                 ih: handle;
  101.                 box: rect;
  102.                 oldtext: str255;
  103.         begin
  104.             GetDItem(dlg, item, it, ih, box);
  105.             GetIText(ih, oldtext);
  106.             if oldtext <> text then
  107.                 SetIText(ih, text);
  108.         end;
  109.  
  110.         procedure SetMyTurn;
  111. { Set ger.myturn, and fix up any displayed controls (eg White's Turn staus messages) }
  112.             procedure SetStatus (index: integer);
  113.                 var
  114.                     s: str255;
  115.             begin
  116.                 GetIndString(s, game_strh, index);
  117.                 SetItemText(gwindow, game_status_item, s);
  118.             end;
  119.         begin
  120.             ger.myturn := ggame^.playing and ggame^.myturn;
  121.             if ggame^.playing then
  122.                 if ggame^.connected then
  123.                     if ggame^.myturn then
  124.                         SetStatus(status_you)
  125.                     else
  126.                         SetStatus(status_waiting)
  127.                 else
  128.                     SetStatus(status_click);
  129.         end;
  130.  
  131.         procedure DoMove (themove: integer);
  132. { Change parameters to define the move, and do the indicated move }
  133. { Call this in response to your player's moves and in responce to HandleMessage moves }
  134.             var
  135.                 old: rects;
  136.         begin
  137.             ger.modified := true;
  138.             old := ggame^.state;
  139.             ggame^.state := themove;
  140.             DrawTheRect(ggame, old);
  141.             DrawTheRect(ggame, themove);
  142.             SetMyTurn;
  143.         end;
  144.  
  145.         procedure SendMove (themove: integer);
  146. { Change parameters to define the move, and send the message defined by the parameters }
  147.         begin
  148.             ger.event := ge_SendMessage;
  149.             ger.message := concat('M', chr(themove));
  150.             ggame^.myturn := false;
  151.             SetMyTurn;
  152.         end;
  153.  
  154.         procedure HandleMessage (s: str255);
  155. { Convert the string to a move and call DoMove }
  156.         begin
  157.             DoMove(ord(s[2]));
  158.             ggame^.myturn := true;
  159.             SetMyTurn;
  160.         end;
  161.  
  162.         procedure DoClick (where: point);
  163. { Handle a user click, and turn it into a move, and then send it }
  164.             var
  165.                 move: integer;
  166.                 i: rects;
  167.         begin
  168.             move := 0;
  169.             with gglobals^ do
  170.                 for i := 1 to rect_max do
  171.                     if PtInRect(where, therects[i]) then
  172.                         move := i;
  173.             if (move <> 0) and (move <> ggame^.state) then begin
  174.                 DoMove(move);
  175.                 if ggame^.connected then
  176.                     SendMove(move);
  177.             end;
  178.         end;
  179.  
  180.         procedure InitRuleBook (var r: rect);
  181. { Initialize the global variables, and return the size of the window }
  182.         begin
  183.         end;
  184.  
  185.         procedure FinishRuleBook;
  186. { Destroy the global variables you created }
  187.         begin
  188.         end;
  189.  
  190.         procedure InitGame;
  191. { Initialize the game variables, and global vars that require the window to be open }
  192.             var
  193.                 i: rects;
  194.                 width, left: integer;
  195.         begin
  196.             with gglobals^ do begin
  197. { Set the rects from item_rect }
  198.                 for i := 1 to rect_max do
  199.                     therects[i] := item_rect;
  200.                 left := item_rect.left;
  201.                 width := item_rect.right - left;
  202.                 therects[1].right := left + width div 4;
  203.                 therects[2].right := left + width div 2;
  204.                 therects[3].right := left + 3 * width div 4;
  205.                 for i := 2 to rect_max do
  206.                     therects[i].left := therects[i - 1].right;
  207.             end;
  208.             ggame^.myturn := true;
  209.         end;
  210.  
  211.         procedure RestartGame;
  212. { Reset all the vars in the ggame record }
  213.         begin
  214.             ggame^.state := 1;
  215.             ger.modified := false;
  216.         end;
  217.  
  218.         procedure Swap;
  219. { Swap the side you are playing (eg from White to Black).  Don't swap which colour is to }
  220. { play.  Thus, swap myturn. }
  221.         begin
  222.             ggame^.myturn := not ggame^.myturn;
  223.         end;
  224.  
  225.         procedure ConnectionLost;
  226. { The connection has been broken }
  227.         begin
  228.         end;
  229.  
  230.         procedure ConnectionMade;
  231. { A new connection has been made }
  232.         begin
  233.         end;
  234.  
  235. { Touch Not: Generic routines }
  236.  
  237.         procedure GInitRuleBook;
  238. { Touch Not: Creates gglobals }
  239.             var
  240.                 r: rect;
  241.                 h: handle;
  242.         begin
  243.             ger.globals := handle(NewPtr(sizeof(globalsRecord)));
  244.             gglobals := globalsPeek(ger.globals);
  245.             h := Get1Resource('DLOG', game_dlog);
  246.             if h = nil then
  247.                 SetRect(r, 0, 0, 100, 100)
  248.             else begin
  249.                 BlockMove(h^, @r, SizeOf(rect));
  250.                 OffsetRect(r, -r.left, -r.top);
  251.             end;
  252.             InitRuleBook(r);
  253.             with r do begin
  254.                 ger.int1 := left + right;
  255.                 ger.int2 := top + bottom;
  256.             end;
  257.         end;
  258.  
  259.         procedure GFinishRuleBook;
  260. { Touch Not: Destroy gglobals }
  261.         begin
  262.             FinishRuleBook;
  263.             DisposPtr(ptr(ger.globals));
  264.             ger.globals := nil;
  265.             gglobals := nil;
  266.         end;
  267.  
  268.         procedure GCommonInit;
  269. { Touch Not: Handle both New and Open game by reinitializing some game variables }
  270.             var
  271.                 k: integer;
  272.                 h: handle;
  273.         begin
  274.             GetDItem(gwindow, game_dialog_item, k, h, gglobals^.item_rect);
  275.             SetDItem(gwindow, game_dialog_item, k, handle(@DrawGameProc), gglobals^.item_rect);
  276.             SetWRefCon(gwindow, longInt(ghandle));
  277.             ggame^.globals := gglobals;
  278.             ggame^.connected := false;
  279.             InitGame;
  280.         end;
  281.  
  282.         procedure GRestartGame;
  283. { Touch Not: Reset the game to the initial state.  Don't change the side you are playing }
  284.         begin
  285.             RestartGame;
  286.             SetMyTurn;
  287.             RedrawGame;
  288.         end;
  289.  
  290.         procedure GNewGame;
  291. { Touch Not: Set the ggame handle size, and initialize all the ggame variables. }
  292.         begin
  293.             HUnlock(ghandle);
  294.             SetHandleSize(ghandle, SizeOf(gameRecord));
  295.             HLock(ghandle);
  296.             ggame := gamePeek(ghandle^);
  297.             ggame^.playing := true;
  298.             GCommonInit;
  299.             GRestartGame;
  300.         end;
  301.  
  302.         procedure GOldGame;
  303. { Touch Not: Initialize some of the ggame vars }
  304.         begin
  305.             GCommonInit;
  306.             SetMyTurn;
  307.             RedrawGame;
  308.         end;
  309.  
  310.         procedure GSwap;
  311. { Touch Not: Swap which side you are playing }
  312.         begin
  313.             Swap;
  314.             SetMyTurn;
  315.         end;
  316.  
  317.         procedure GConnectionLost;
  318. { Touch Not:  The connection has been broken }
  319.         begin
  320.             ggame^.connected := false;
  321.             ConnectionLost;
  322.             SetMyTurn;
  323.         end;
  324.  
  325.         procedure GConnectionMade;
  326. { Touch Not:  A new connection has been made }
  327.         begin
  328.             ggame^.connected := true;
  329.             ConnectionMade;
  330.             SetMyTurn;
  331.         end;
  332.  
  333.         procedure GRestart;
  334. { Touch Not:  Restart the game from scratch.  Don't change which side you are playing }
  335.             var
  336.                 r: rect;
  337.         begin
  338.             RestartGame;
  339.             SetMyTurn;
  340.             RedrawGame;
  341.         end;
  342.  
  343.         procedure GMouseDown;
  344. { Touch Not:  Handle clicks }
  345.         begin
  346.             DoClick(ger.where);
  347.         end;
  348.  
  349.         procedure GMessageReceived;
  350. { Touch Not:  Handle inbound messages }
  351.         begin
  352.             HandleMessage(ger.message);
  353.         end;
  354.  
  355.     begin
  356.         gglobals := globalsPeek(ger.globals);
  357.         ghandle := ger.game;
  358.         if ghandle <> nil then begin
  359.             HLock(ghandle);
  360.             ggame := gamePeek(ghandle^);
  361.         end;
  362.         GetPort(gwindow);
  363.         case ger.event of
  364.             ge_InitRuleBook: 
  365.                 GInitRuleBook;
  366.             ge_FinishRuleBook: 
  367.                 GFinishRuleBook;
  368.             ge_NewGame: 
  369.                 GNewGame;
  370.             ge_OldGame: 
  371.                 GOldGame;
  372.             ge_ConnectionLost: 
  373.                 GConnectionLost;
  374.             ge_ConnectionMade: 
  375.                 GConnectionMade;
  376.             ge_MessageReceived: 
  377.                 GMessageReceived;
  378.             ge_MouseDown: 
  379.                 GMouseDown;
  380.             ge_Swap: 
  381.                 GSwap;
  382.             ge_Restart: 
  383.                 GRestart;
  384.             otherwise
  385.         end;
  386.         if ghandle <> nil then
  387.             HUnlock(ghandle);
  388.     end;
  389.  
  390. end.